#include <stdint-gcc.h>

#include "uart.h"

void uart_init(void)
{
	// 工作模式：115200 8N1 @ 100MHz
	uart_lcr = 0x83;
	uart_dl  = 54;
	uart_lcr = 0x03;
}

void uart_print(const char *s)
{
	while (*s) {
		while (!(uart_lsr&(1<<5)));
		uart_thr = *s++;
	}
}

void uart_recv(uint8_t *buf, int n)
{
	while (n > 0) {
		while (!(uart_lsr&1));
		*buf++ = uart_rbr;
		n--;
	}
}

uint8_t uart_recv_byte(void)
{
	uint8_t c;
	while (!(uart_lsr&1));
	c = uart_rbr;
	return c;
}

void uart_send_byte(uint8_t c)
{
	while (!(uart_lsr&(1<<5)));
	uart_thr = c;
}

int putchar(int c)
{
	while (!(uart_lsr&(1<<5)));
	return uart_thr = c;
}

int puts(const char *s)
{
	while (*s) {
		putchar(*s++);
	}
	putchar('\n');
}
